/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
  ListNode* swapPairs(ListNode* head) {
    auto dummy = new ListNode(0, head);
    auto cur = dummy;
    while (cur->next && cur->next->next) {
      auto n1 = cur->next;
      auto n2 = n1->next;
      auto n3 = n2->next;
      cur->next = n2;
      n1->next = n3;
      n2->next = n1;
      cur = n1;
    }
    return dummy->next;
  }
};